package elitech.vietnam.myfashion.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.OrderDetail;
import elitech.vietnam.myfashion.view.OrderDetailView.OrderDetailViewEvent;
import elitech.vietnam.myfashion.wsclient.Utilities;

public class OrderView extends LinearLayout {

	private MainActivity mContext;
	private TextView mTxtCode, mTxtDate, mTxtStatus, mTxtShipping, mTxtTotal;
	private LinearLayout mLayOrder, mlayDate, mlayStatus;
	public LinearLayout mlayTitle, mLayShipping, mlayOrder, mlayControl, mlayCancel;
	private Button mBtnCancel, mBtnPurchase, mBtnClear;
	public double mTotal;
	public Order mOrder;
	
	public OrderView(final MainActivity context, Order item) {
		super(context);
		this.mContext = context;
		this.mOrder = item;
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.item_order, this);
		}

		mTxtCode = (TextView) findViewById(R.id.order_txtCode);
		mTxtDate = (TextView) findViewById(R.id.order_txtDate);
		mTxtShipping = (TextView) findViewById(R.id.order_txtShipping);
		mTxtTotal = (TextView) findViewById(R.id.order_txtTotal);
		mTxtStatus = (TextView) findViewById(R.id.order_txtStatus);
		mlayTitle = (LinearLayout) findViewById(R.id.order_layTitle);
		mlayControl = (LinearLayout) findViewById(R.id.order_layControl);
		mlayCancel = (LinearLayout) findViewById(R.id.order_layCancel);
		mLayOrder = (LinearLayout) findViewById(R.id.order_layOrder);
		mLayShipping = (LinearLayout) findViewById(R.id.order_layShipping);
		mlayDate = (LinearLayout) findViewById(R.id.order_layDate);
		mlayStatus = (LinearLayout) findViewById(R.id.order_layStatus);
		mBtnClear = (Button) findViewById(R.id.order_btnClear);
		mBtnPurchase = (Button) findViewById(R.id.order_btnPurchase);
		mBtnCancel = (Button) findViewById(R.id.order_btnCancel);
		
		mTxtCode.setText(item.OrderCode);
		mTxtDate.setText(item.date);

		mBtnClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				event.onClear();
			}
		});
		
		mBtnPurchase.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				event.onPurchase();
			}
		});

		mBtnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				event.onCancel();
			}
		});
		
		if(item.Status!=-100){
			mlayDate.setVisibility(View.VISIBLE);
			mlayStatus.setVisibility(View.VISIBLE);
			
//			if(item.Ship != 0){
				mLayShipping.setVisibility(View.VISIBLE);
				mTxtShipping.setText(Utilities.numberFormat(item.Ship) + " VNĐ");
//			}
			
			if(item.Status==0){
				mTxtStatus.setText(R.string.uncofirm);
				mTxtStatus.setTextColor(getResources().getColor(R.color.red));
				mlayCancel.setVisibility(View.VISIBLE);
			}else if(item.Status==4){
				mTxtStatus.setText(R.string.success);
				mTxtStatus.setTextColor(getResources().getColor(R.color.green));
				mlayCancel.setVisibility(View.VISIBLE);
				mBtnCancel.setText(R.string.delete);
			}else{
				mTxtStatus.setText(R.string.cofirmed);
				mTxtStatus.setTextColor(getResources().getColor(R.color.green));
			}
			
		}
		
		for (final OrderDetail detail : item.ListDetail) {
			final OrderDetailView view = new OrderDetailView(context, detail);
			
			mTotal = mTotal + (detail.getAmountVN());
			if(item.Status==-100){
				view.mLayDelete.setVisibility(View.VISIBLE);
				view.setOnCancelEvent(new OrderDetailViewEvent() {
					
					@Override
					public void onCancel() {
						if (mOrder.ListDetail.size() > 1) {
							new OkCancelWarnningDialog(mContext, mContext
									.getString(R.string.confirmdeletebill)) {
								public void onOk() {
									mLayOrder.removeView(view);
									mOrder.ListDetail.remove(detail);
									mTotal = mTotal - detail.getAmountVN();
									mTxtTotal.setText(Utilities
											.numberFormat(mTotal) + " VND");
									event.onRemove();
								};
							}.show();
						} else {
							new OkCancelWarnningDialog(mContext, mContext
									.getString(R.string.confirmdeletebill)) {
								public void onOk() {
									mContext.mOrder = new Order();
									event.onRemove();
								};
							}.show();
						}
					}
				});
			}
			mLayOrder.addView(view);
		}
		
		mTotal = mTotal + item.Ship;
		
		String total = (mTotal == 0)? Utilities.numberFormat(mTotal) + " VNĐ": Utilities.numberFormat(mTotal) + " VNĐ";
		mTxtTotal.setText(total);
	}
	
	private OrderViewEvent event;

	public void setEvent(OrderViewEvent mEvent) {
		this.event = mEvent;
	}

	public interface OrderViewEvent {
		public void onClear();
		public void onPurchase();
		public void onRemove();
		public void onCancel();
	};

}
